package com.union.core.organization.action;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.union.core.organization.model.OrganizationManager;
import com.union.core.organization.model.OrganizationParam;
import com.union.core.organization.model.Personnel;
import com.union.core.organization.service.OrganizationManagerService;
import com.union.core.organization.service.OrganizationParamService;
import com.union.core.organization.service.PersonnelService;
import com.union.core.sys.service.SysLogsService;
import com.union.core.sys.service.SysManagerService;
import com.union.core.sys.service.SysService;

public class PersonnelAction extends ActionSupport{
	
	private static final long serialVersionUID = 872316012305159L;
	private SysService sysService = SysService.getInstance();
	
	private static Logger log = Logger.getLogger(PersonnelAction.class.getName());
	
	private PersonnelService personnelService;
	private List<Personnel> personnelList;
	private Personnel personnel;
	private Personnel personnel1;
	
	public Personnel getPersonnel1() {
		return personnel1;
	}


	public void setPersonnel1(Personnel personnel1) {
		this.personnel1 = personnel1;
	}


	private OrganizationManagerService organizationService;
	private List<OrganizationManager> OrganizationList;
	private OrganizationManager organization;
	
	private OrganizationParamService paramService;
	private List<OrganizationParam> ParamList2; 
	private List<OrganizationParam> ParamList3; 
	private List<OrganizationParam> ParamList4; 
	private OrganizationParam param;
	
	//人员信息
	private int personnel_id;       
	private int personnel_id1; 
	private String personnel_name;       // 姓名      
	private String birthday;             // 出生日期     
	private String personnel_sex;        // 性别      
	private String personnel_phone;      // 联系电话
	private String personnel_address;    // 地址      
	private String personnel_position;   // 职务      
	private int organization_id;         // 机构id    
	private String organization_name;    // 机构名称
	private int pid;                     // 父机构id 
	private String organization_parent;  // 父机构   
	private String add_name;             // 添加人   
	private String add_time;             // 添加时间
	private String updae_name;           // 修改人   
	private String update_time;          // 修改时间
	private String remarks;              // 简历
	private String department;           //部门      
	private String email;                //电子邮件
	private String nation;               // 民族      
	private String political_status;      //政治面貌
	private String educational_background;  // 学历      
	private String major;                   //专业      
	private String native_place;            // 籍贯      
	private String id_num;                  //身份证号
	private String issuing_authority;       //发证机关
	private int zoneunion;
	private int streetunion;
	private int localunion;
	
	//分页
	private int start;
	private int intPageSize =15; //每页行数
	private int page = 1;      //页码
	private int intRowCount;   //总行数
	private int intPageCount;  //总页数
	private int intPage;       //页数变量
	private ByteArrayInputStream inputStream;
	private String paramName;
	private String paramValue;
	private String orderByName;
	//账号
	private SysManagerService sysManagerSer;
	//日志
	private SysLogsService logsSer;
	//权限控制
	private List<String> allPower;

    private int p;

	public PersonnelAction(){
		personnelService = new PersonnelService();
		personnelList = new ArrayList<Personnel>();
		personnel = new Personnel();
		personnel1 = new Personnel();
		
		organizationService = new OrganizationManagerService();
		OrganizationList = new ArrayList<OrganizationManager>();
		organization = new OrganizationManager();
		
		paramService= new OrganizationParamService();
		ParamList2 = new ArrayList<OrganizationParam>();
		ParamList3 = new ArrayList<OrganizationParam>();
		ParamList4 = new ArrayList<OrganizationParam>();
		param = new OrganizationParam();
		
		sysManagerSer=new SysManagerService();
		logsSer = new SysLogsService();
	}
	
	
	public String queryList(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";
		}else{
			
				allPower=sysService.getSessionListener().getAllPower();
				personnel_id = sysService.getSessionListener().getPERSONNEL_ID();
				if(sysService.getSessionListener().getPERSONNEL().getOrganization_id()==1){		
				organization_id=1;
				lookup();
				setQuerySession();
				//lookForPerson();
				}else{
					if(personnel_id!=0){
						personnel = personnelService.query(personnel_id);
						organization_id =personnel.getOrganization_id();
						lookup();
						setQuerySession();
						//lookForPerson();
					}
				}
				
				//lookup();
				//setQuerySession();

				return "SUCCESS";
		}	
	}

	/**
	 * 查询列表(多页)
	 * @return String
	 */
	public String pagelist(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			allPower=sysService.getSessionListener().getAllPower();
			personnel = sysService.getSessionListener().getPERSONNEL();
			if(personnel.getOrganization_id()==1){
				organization_id=0;
			}else{
				organization_id =personnel.getOrganization_id();
			}
			getQuerySession();  //get the query sessions before lookup!
			lookup();
			return "SUCCESS";
		}
	}
	
	/*
	 * 加载添加机构
	 */
	public String addload(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
//			OrganizationList = organizationService.queryList("海口市总工会");
//			
//			personnel = sysService.getSessionListener().getPERSONNEL();
//			personnel_id = sysService.getSessionListener().getPERSONNEL_ID();
//			if(personnel.getOrganization_id()==1){
//				p=1;
//			}
			return "SUCCESS";
		}
	}
	
	/*
	 * 添加机构
	 */
	public String add(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			
			personnel.setPersonnel_name(personnel_name);
			personnel.setPersonnel_sex(personnel_sex);
			personnel.setBirthday(birthday);
			personnel.setPersonnel_address(personnel_address);
			personnel.setPersonnel_phone(personnel_phone);
			personnel.setPersonnel_position(personnel_position);
			personnel.setRemarks(remarks);
			personnel.setDepartment(department);
			personnel.setEducational_background(educational_background);
			personnel.setEmail(email);
			personnel.setId_num(id_num);
			personnel.setIssuing_authority(issuing_authority);
			personnel.setMajor(major);
			personnel.setNation(nation);
			personnel.setNative_place(native_place);
			personnel.setPolitical_status(political_status);
			organization = organizationService.query(organization_id);
			personnel.setOrganization_name(organization.getOrganization_name());
			personnel.setOrganization_id(organization_id);
			personnel.setOrganization_parent(organization.getOrganization_parent());
			personnel.setPid(organization.getPid());
//			if(p==1){
//				if(localunion!=0){
//				organization = organizationService.query(localunion);
//				personnel.setOrganization_name(organization.getOrganization_name());
//				personnel.setOrganization_id(localunion);
//				personnel.setOrganization_parent(organization.getOrganization_parent());
//				personnel.setPid(organization.getPid());
//				}
//				else{
//					if(streetunion!=0){
//						organization = organizationService.query(streetunion);
//						personnel.setOrganization_name(organization.getOrganization_name());
//						personnel.setOrganization_id(streetunion);
//						personnel.setOrganization_parent(organization.getOrganization_parent());
//						personnel.setPid(organization.getPid());
//					}
//					else{
//						if(zoneunion!=0){
//							organization = organizationService.query(zoneunion);
//							personnel.setOrganization_name(organization.getOrganization_name());
//							personnel.setOrganization_id(zoneunion);
//							personnel.setOrganization_parent(organization.getOrganization_parent());
//							personnel.setPid(organization.getPid());
//						}else{
//							personnel.setOrganization_name("海口市总工会");
//							organization = organizationService.query("海口市总工会");
//							personnel.setOrganization_id(organization.getOrganization_id());
//						}
//					}
//				}
//			}else{
////				personnel = personnelService.query(sysService.getSessionListener().getPERSONNEL_ID());
//				personnel1 = sysService.getSessionListener().getPERSONNEL();
//				personnel.setOrganization_name(personnel1.getOrganization_name());
//				personnel.setOrganization_id(personnel1.getOrganization_id());
//				personnel.setOrganization_parent(personnel1.getOrganization_parent());
//				personnel.setPid(personnel1.getPid());
//			}
			
			personnel.setAdd_name(sysService.getSessionListener().getMANAGER_NAME());
			personnelService.add(personnel);
			logsSer.addSysLogs("添加人员【"+personnel_name+"】");
			return "SUCCESS";
		}
	}


	/*
	 * 加载修改机构
	 */
	public String editload(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			personnel = personnelService.query(personnel_id);
			organization = organizationService.query(personnel.getOrganization_id());
			ParamList2 = paramService.queryList(2);
			ParamList3 = paramService.queryList(3);
			ParamList4 = paramService.queryList(4);
			
			personnel1 = sysService.getSessionListener().getPERSONNEL();
			personnel_id1 = sysService.getSessionListener().getPERSONNEL_ID();
			if(personnel1.getOrganization_id()==1){
				p=1;
			}
			
			return "SUCCESS";
		}
	}
	
	/*
	 * 查询机构信息
	 */
	public String query(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			personnel = personnelService.query(personnel_id);
			return "SUCCESS";
		}
	}
	
	/*
	 * 修改人员信息
	 */
	public String edit(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			personnel.setPersonnel_address(personnel_address);
			personnel.setBirthday(birthday);
			personnel.setPersonnel_name(personnel_name);
			personnel.setPersonnel_phone(personnel_phone);
			personnel.setPersonnel_position(personnel_position);
			personnel.setPersonnel_sex(personnel_sex);
			personnel.setRemarks(remarks);
			personnel.setDepartment(department);
			personnel.setEducational_background(educational_background);
			personnel.setEmail(email);
			personnel.setId_num(id_num);
			personnel.setIssuing_authority(issuing_authority);
			personnel.setMajor(major);
			personnel.setNation(nation);
			personnel.setNative_place(native_place);
			personnel.setPolitical_status(political_status);
			personnel.setPersonnel_id(personnel_id);
			personnel1 = personnelService.query(personnel_id);
			personnel.setOrganization_id(personnel1.getOrganization_id());
			personnel.setOrganization_name(personnel1.getOrganization_name());
			personnel.setOrganization_parent(personnel1.getOrganization_parent());
			personnel.setPid(personnel1.getPid());

			personnel.setUpdae_name(sysService.getSessionListener().getMANAGER_NAME());
			personnelService.edit(personnel);
			logsSer.addSysLogs("修改人员【"+personnel_name+"】");
			return "SUCCESS";
		}
	}
	
	/*
	 * 删除机构信息
	 */
	public String delete(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			personnel=personnelService.query(personnel_id);
			int accountId=personnel.getAccount_id();
			personnelService.delete(personnel_id);
			//同时删除对应账号信息
			if(accountId!=0){
				sysManagerSer.deleteSysManager(accountId);
			}
			logsSer.addSysLogs("删除人员【"+personnel.getPersonnel_name()+"】");
			return "SUCCESS";
		}
	}
	
	private void lookup(){
		
		if("".equals(paramName))paramName = null;
		if("".equals(paramValue))paramValue = null;
		if("".equals(orderByName))orderByName = null;
		
		if(page<1){
			page = 1;
			intPage = 1;
			page = 1;
		}else{
			intPage = page;
		}
		System.out.println("aaaaa"+organization_id);
		personnel = personnelService.query1(organization_id);
		String   s = personnel.getChildLst(); 
        String   getChildLst =   "";  
        getChildLst = s.replaceAll( "\\$,","");
        System.out.println("cccc"+getChildLst);
		intRowCount = personnelService.ListCount(0, null, null, null, 
				null, null, null, paramName, paramValue ,0,getChildLst);
		intPageCount = (intRowCount + intPageSize -1)/intPageSize;
		if(intPage > intPageCount) intPage = intPageCount;		
		if(intPageCount>0){
			start = (intPage-1) * intPageSize;
			personnelList = personnelService.queryList(0,null, null, null,
					null, null, null, paramName, paramValue, orderByName, start, intPageSize,0,getChildLst);
	    }
	}
	
	private void setQuerySession(){
		ServletActionContext.getRequest().getSession().setAttribute("personnel_id",personnel_id); 
		ServletActionContext.getRequest().getSession().setAttribute("personnel_name",personnel_name); 
		ServletActionContext.getRequest().getSession().setAttribute("birthday",birthday); 
		ServletActionContext.getRequest().getSession().setAttribute("personnel_sex",personnel_sex); 
		ServletActionContext.getRequest().getSession().setAttribute("personnel_position",personnel_position);
		ServletActionContext.getRequest().getSession().setAttribute("organization_id",organization_id); 
		ServletActionContext.getRequest().getSession().setAttribute("organization_name",organization_name); 
		ServletActionContext.getRequest().getSession().setAttribute("department",department); 
		ServletActionContext.getRequest().getSession().setAttribute("paramName",paramName); 
		ServletActionContext.getRequest().getSession().setAttribute("paramValue",paramValue); 
		ServletActionContext.getRequest().getSession().setAttribute("orderByName",orderByName); 
		
	}
	
	private void getQuerySession(){
		personnel_id = (Integer)ServletActionContext.getRequest().getSession().getAttribute("personnel_id");
		personnel_name = (String)ServletActionContext.getRequest().getSession().getAttribute("personnel_name");
		birthday = (String)ServletActionContext.getRequest().getSession().getAttribute("birthday");
		personnel_sex = (String)ServletActionContext.getRequest().getSession().getAttribute("personnel_sex");
		personnel_position = (String)ServletActionContext.getRequest().getSession().getAttribute("personnel_position");
		organization_id = (Integer)ServletActionContext.getRequest().getSession().getAttribute("organization_id");
		organization_name = (String)ServletActionContext.getRequest().getSession().getAttribute("organization_name");
		department = (String)ServletActionContext.getRequest().getSession().getAttribute("department");
		paramName = (String)ServletActionContext.getRequest().getSession().getAttribute("paramName");
		paramValue = (String)ServletActionContext.getRequest().getSession().getAttribute("paramValue");
		orderByName = (String)ServletActionContext.getRequest().getSession().getAttribute("orderByName");
	}
	
	public static Logger getLog() {
		return log;
	}


	public static void setLog(Logger log) {
		PersonnelAction.log = log;
	}


	public List<Personnel> getPersonnelList() {
		return personnelList;
	}


	public void setPersonnelList(List<Personnel> personnelList) {
		this.personnelList = personnelList;
	}


	public Personnel getPersonnel() {
		return personnel;
	}


	public void setPersonnel(Personnel personnel) {
		this.personnel = personnel;
	}


	public int getPersonnel_id() {
		return personnel_id;
	}


	public void setPersonnel_id(int personnelId) {
		personnel_id = personnelId;
	}


	public String getPersonnel_name() {
		return personnel_name;
	}


	public void setPersonnel_name(String personnelName) {
		personnel_name = personnelName;
	}

	public String getPersonnel_sex() {
		return personnel_sex;
	}


	public void setPersonnel_sex(String personnelSex) {
		personnel_sex = personnelSex;
	}


	public String getPersonnel_phone() {
		return personnel_phone;
	}


	public void setPersonnel_phone(String personnelPhone) {
		personnel_phone = personnelPhone;
	}


	public String getPersonnel_address() {
		return personnel_address;
	}


	public void setPersonnel_address(String personnelAddress) {
		personnel_address = personnelAddress;
	}


	public String getPersonnel_position() {
		return personnel_position;
	}


	public void setPersonnel_position(String personnelPosition) {
		personnel_position = personnelPosition;
	}


	public int getOrganization_id() {
		return organization_id;
	}


	public void setOrganization_id(int organizationId) {
		organization_id = organizationId;
	}


	public String getOrganization_name() {
		return organization_name;
	}


	public void setOrganization_name(String organizationName) {
		organization_name = organizationName;
	}


	public int getPid() {
		return pid;
	}


	public void setPid(int pid) {
		this.pid = pid;
	}


	public String getOrganization_parent() {
		return organization_parent;
	}


	public void setOrganization_parent(String organizationParent) {
		organization_parent = organizationParent;
	}


	public String getAdd_name() {
		return add_name;
	}


	public void setAdd_name(String addName) {
		add_name = addName;
	}


	public String getAdd_time() {
		return add_time;
	}


	public void setAdd_time(String addTime) {
		add_time = addTime;
	}


	public String getUpdae_name() {
		return updae_name;
	}


	public void setUpdae_name(String updaeName) {
		updae_name = updaeName;
	}


	public String getUpdate_time() {
		return update_time;
	}


	public void setUpdate_time(String updateTime) {
		update_time = updateTime;
	}


	public String getRemarks() {
		return remarks;
	}


	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}


	public int getStart() {
		return start;
	}


	public void setStart(int start) {
		this.start = start;
	}


	public int getIntPageSize() {
		return intPageSize;
	}


	public void setIntPageSize(int intPageSize) {
		this.intPageSize = intPageSize;
	}


	public int getPage() {
		return page;
	}


	public void setPage(int page) {
		this.page = page;
	}


	public int getIntRowCount() {
		return intRowCount;
	}


	public void setIntRowCount(int intRowCount) {
		this.intRowCount = intRowCount;
	}


	public int getIntPageCount() {
		return intPageCount;
	}


	public void setIntPageCount(int intPageCount) {
		this.intPageCount = intPageCount;
	}


	public int getIntPage() {
		return intPage;
	}


	public void setIntPage(int intPage) {
		this.intPage = intPage;
	}


	public ByteArrayInputStream getInputStream() {
		return inputStream;
	}


	public void setInputStream(ByteArrayInputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getParamName() {
		return paramName;
	}


	public void setParamName(String paramName) {
		this.paramName = paramName;
	}


	public String getParamValue() {
		return paramValue;
	}


	public void setParamValue(String paramValue) {
		this.paramValue = paramValue;
	}


	public String getOrderByName() {
		return orderByName;
	}


	public void setOrderByName(String orderByName) {
		this.orderByName = orderByName;
	}


	public OrganizationManager getOrganization() {
		return organization;
	}


	public void setOrganization(OrganizationManager organization) {
		this.organization = organization;
	}


	public List<OrganizationManager> getOrganizationList() {
		return OrganizationList;
	}


	public void setOrganizationList(List<OrganizationManager> organizationList) {
		OrganizationList = organizationList;
	}

	public String getDepartment() {
		return department;
	}


	public void setDepartment(String department) {
		this.department = department;
	}


	public String getEmail() {
		return email;
	}


	public void setEmail(String email) {
		this.email = email;
	}


	public String getNation() {
		return nation;
	}


	public void setNation(String nation) {
		this.nation = nation;
	}


	public String getPolitical_status() {
		return political_status;
	}


	public void setPolitical_status(String politicalStatus) {
		political_status = politicalStatus;
	}


	public String getEducational_background() {
		return educational_background;
	}


	public void setEducational_background(String educationalBackground) {
		educational_background = educationalBackground;
	}


	public String getMajor() {
		return major;
	}


	public void setMajor(String major) {
		this.major = major;
	}


	public String getNative_place() {
		return native_place;
	}


	public void setNative_place(String nativePlace) {
		native_place = nativePlace;
	}


	public String getId_num() {
		return id_num;
	}


	public void setId_num(String idNum) {
		id_num = idNum;
	}


	public String getIssuing_authority() {
		return issuing_authority;
	}


	public void setIssuing_authority(String issuingAuthority) {
		issuing_authority = issuingAuthority;
	}


	public String getBirthday() {
		return birthday;
	}


	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}


	public void setZoneunion(int zoneunion) {
		this.zoneunion = zoneunion;
	}


	public void setStreetunion(int streetunion) {
		this.streetunion = streetunion;
	}


	public void setLocalunion(int localunion) {
		this.localunion = localunion;
	}


	public int getZoneunion() {
		return zoneunion;
	}


	public int getStreetunion() {
		return streetunion;
	}


	public int getLocalunion() {
		return localunion;
	}


	public List<OrganizationParam> getParamList2() {
		return ParamList2;
	}


	public void setParamList2(List<OrganizationParam> paramList2) {
		ParamList2 = paramList2;
	}


	public List<OrganizationParam> getParamList3() {
		return ParamList3;
	}


	public void setParamList3(List<OrganizationParam> paramList3) {
		ParamList3 = paramList3;
	}


	public List<OrganizationParam> getParamList4() {
		return ParamList4;
	}


	public void setParamList4(List<OrganizationParam> paramList4) {
		ParamList4 = paramList4;
	}


	public OrganizationParam getParam() {
		return param;
	}


	public void setParam(OrganizationParam param) {
		this.param = param;
	}
	public List<String> getAllPower() {
		return allPower;
	}


	public void setAllPower(List<String> allPower) {
		this.allPower = allPower;
	}


	public int getP() {
		return p;
	}


	public void setP(int p) {
		this.p = p;
	}


	public int getPersonnel_id1() {
		return personnel_id1;
	}


	public void setPersonnel_id1(int personnelId1) {
		personnel_id1 = personnelId1;
	}


	
	
	
	
}